# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from google.appengine.ext import db

class Tag(db.Model):
    name = db.StringProperty(required=True)
    qnumber = db.IntegerProperty(default=0)
    
    def questions(self):
        #for q in self.tagquestion_set:
        #    logging.info(q.question.title)
        return DBLayer.getQuestions(self)
    
class QuestionType:
    (Single,Multiple,Text) = range(0,3)
    
class QuestionStatus:
    (Inuse,Deprecate) = range(0,2)

class Question(db.Model):
    title = db.TextProperty()
    type = db.IntegerProperty(default=QuestionType.Single)  #single,multiple,text
    status = db.IntegerProperty(default=QuestionStatus.Inuse) #inuse,deprecate
    seq = db.IntegerProperty()
    def answers(self):
      return self.answer_set

class TagQuestion(db.Model):
    question = db.ReferenceProperty(Question)
    tag = db.ReferenceProperty(Tag)

class Answer(db.Model):
    question = db.ReferenceProperty(Question)
    text = db.StringProperty(required=True)
    isRight = db.BooleanProperty(default=False)
    
class Player(db.Model):
    uid = db.StringProperty()
    name = db.StringProperty()
    photoUrl = db.StringProperty()
    score = db.IntegerProperty(default=0)
    
class Playlog(db.Model):
    uid = db.StringProperty()
    time = db.DateTimeProperty()
    tag = db.ReferenceProperty(Tag)
    answered = db.IntegerProperty(default=0) #user already answer question
    right = db.IntegerProperty(default=0) #user right answer number
    score = db.IntegerProperty(default=0)

class DBLayer:
    @staticmethod
    def getAllTag():
        result = db.GqlQuery("SELECT * FROM Tag ")
        return result
    
    @staticmethod
    def getTag(name):
        result = db.GqlQuery("SELECT * FROM Tag WHERE name = :1",name)
        return result.get()
    
    @staticmethod
    def getQuestions(tag):
        result = tag.tagquestion_set
        return [tq.question for tq in result]
        
    @staticmethod
    def getProfile(uid):
        result = db.GqlQuery("SELECT * FROM Playlog WHERE uid  = :1",uid)
        return result
        
    @staticmethod
    def getPlaylog(uid,tag):
        log = db.GqlQuery("SELECT * FROM Playlog WHERE uid = :1 AND tag = :2",uid,tag)
        return log.get()
    
    @staticmethod
    def getPlayer(uid):
        result = db.GqlQuery("SELECT * FROM Player WHERE uid = :1",uid)
        return result.get()
        
    @staticmethod
    def getNextQuestion(uid,tag):
        log = DBLayer.getPlaylog(uid,tag)
        if not log:
          return tag.questions()[0]
          
        qn = log.answered
        filterlist = [q for q in tag.questions() if q.seq > qn]
        if filterlist:
          return filterlist[0]
        else:
          return None
        
    @staticmethod
    def getRank(ids):
        result = db.GqlQuery("SELECT * FROM Player WHERE uid IN :1 order by score desc",ids)
        return result
        
    @staticmethod
    def getTagRank(ids,tag):
        result = db.GqlQuery("SELECT * FROM Playlog WHERE tag=:1 and uid IN :1 order by score desc",ids,tag)
        return result